import * as vscode from 'vscode';
import { kebabCase, pascalCase } from 'change-case'

export function createCvCt(name: string) {
    return `
<template>
  <div class="component-${kebabCase(name)}">

  </div>
</template>

<script lang="ts" setup>
import dayjs from 'dayjs'

defineOptions({ name: '${name}' })

</script>

<style scoped></style>
`.trimStart();
}

export function createCIdxCt(basename: string) {
    return `
export { default as ${basename} } from './src/${basename}.vue';
`.trimStart();
}

export async function createCc(targetPath: vscode.Uri) {
    try {
        // 若已存在则报错，避免无意覆盖
        await vscode.workspace.fs.stat(targetPath);
        throw new Error('目标文件已存在');
    } catch (err: any) {
        if (err?.code === 'FileNotFound' || /ENOENT/i.test(String(err?.message))) {
            const basename = targetPath.path.split('/').pop();
            if (!basename) {
                throw new Error('未获取到文件名');
            }
            await vscode.workspace.fs.writeFile(vscode.Uri.joinPath(targetPath, `src/${basename}.vue`), Buffer.from(createCvCt(basename), 'utf8'));
            await vscode.workspace.fs.writeFile(vscode.Uri.joinPath(targetPath, 'index.ts'), Buffer.from(createCIdxCt(basename), 'utf8'));
            return;
        }
        throw err;
    }
}
